<script setup>
  import { ref, onMounted } from 'vue'
  import { MinusOutlined, PlusOutlined } from '@ant-design/icons-vue'
  import { editCartGoodsApi } from "@/api"
  const props = defineProps({
    number:{type:Number,required:true,default:0},
    sku:{type:String,required:true,default:''},
  })
  const num = ref(1)
  const minus = () =>{
    if(num.value === 1) return
    num.value = num.value - 1
    editCartGoods()
  }
  const plus = () => {
    num.value = num.value + 1
    editCartGoods()
  }
  const editCartGoods = () => {
    editCartGoodsApi({sku: props.sku, num: num.value}).then((res) => {
      console.log(res)
    })
  }
  onMounted(() => {
    setTimeout(() => {
      num.value = props.number
    }, 100)
  })
</script>

<template>
  <a-input-number v-model:value="num" :bordered="false" :min="1" :controls="false" decimalSeparator="0">
    <template #addonBefore>
      <a-button @click="minus" :disabled="num===1">
        <MinusOutlined/>
      </a-button>
    </template>
    <template #addonAfter>
      <a-button @click="plus">
        <PlusOutlined/>
      </a-button>
    </template>
  </a-input-number>
</template>

<style scoped lang="scss">
  :deep(.ant-input-number) {
    width: 40px;
  }
  :deep(.ant-input-number-group-addon) {
    background-color: transparent;
    border: none;
    padding: 0;
  }
  :deep(.ant-input-number-input) {
    text-align: center !important;
  }
  .ant-btn {
    width: 32px;
    height: 32px;
    padding: 0;
    color: #999;
    border: none;
    border-radius: 10px;
    background-color: #f1f1f5;
    display: flex;
    align-items: center;
    justify-content: center;
    &:hover {
      background-color: #e8e8f0;
    }
    .anticon {
      color: #242424;
      font-size: 16px;
    }
  }
</style>